package com.framework.loippi.plugin.alipay;


import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.demo.trade.config.Configs;
import com.alipay.demo.trade.model.builder.AlipayTradePrecreateRequestBuilder;
import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;
import com.alipay.demo.trade.service.AlipayTradeService;
import com.alipay.demo.trade.service.impl.AlipayTradeServiceImpl;
import com.framework.loippi.cache.ConfigCache;

/**
 * @version V1.0
 * @Description: 支付宝扫码支付
 * @author: wangcj
 * @date: 2017年6月24日 下午4:10:51
 */
public class ALiPayPluginService {

    private static AlipayTradeService tradeService;

    static {
        /** 一定要在创建AlipayTradeService之前调用Configs.init()设置默认参数
         *  Configs会读取classpath下的zfbinfo.properties文件配置信息，如果找不到该文件则确认该文件是否在classpath目录
         */
        //Configs.init("zfbinfo.properties");

        /** 使用Configs提供的默认参数
         *  AlipayTradeService可以使用单例或者为静态成员对象，不需要反复new
         */
        tradeService = new AlipayTradeServiceImpl.ClientBuilder().build();

    }

    public static String getPayCodeUrl(PayParameter payParameter) {

        // 一定要在创建AlipayTradeService之前设置参数
//        Configs.init("zfbinfo.properties");
//        AlipayTradeService tradeService = new AlipayTradeServiceImpl.ClientBuilder().build();

        AlipayTradePrecreateRequestBuilder builder = new AlipayTradePrecreateRequestBuilder()
                .setSubject(payParameter.getSubject())
                .setTotalAmount(payParameter.getTotalAmount())
                .setOutTradeNo(payParameter.getOutTradeNo())
                .setUndiscountableAmount(payParameter.getUndiscountableAmount())
                .setSellerId(payParameter.getSellerId())
                .setBody(payParameter.getBody())
                .setOperatorId(payParameter.getOperatorId())
                .setStoreId(payParameter.getStoreId())
//                .setExtendParams(extendParams)
                .setTimeoutExpress(payParameter.getTimeoutExpress())
                .setNotifyUrl(getNotifyUrl(payParameter.getOutTradeNo()));
//                .setGoodsDetailList(goodsDetailList);

        AlipayF2FPrecreateResult result = tradeService.tradePrecreate(builder);
        String code_url = "";
        switch (result.getTradeStatus()) {
            case SUCCESS:
                //log.info("支付宝预下单成功: )");

                AlipayTradePrecreateResponse res = result.getResponse();
                code_url = res.getQrCode();
                break;

            case FAILED:
                //log.error("支付宝预下单失败!!!");
                code_url = "";
                break;

            case UNKNOWN:
                //log.error("系统异常，预下单状态未知!!!");
                code_url = "";
                break;

            default:
                //log.error("不支持的交易状态，交易返回异常!!!");
                code_url = "";
                break;
        }

        return code_url;

    }

    /**
     * 获取通知URL
     *
     * @param sn 编号
     * @return 通知URL
     */
    protected static String getNotifyUrl(String sn) {
        return "http://guider.carcarshow.net/api/payment/notify/alipay/" + sn + ".json";
    }

}
